package org.csc.sss.api.config;

import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.MultipleKeysDatabaseShardingAlgorithm;

import java.util.Collection;
import java.util.Collections;

public class MyMultipleKeysDatabaseShardingAlgorithm implements MultipleKeysDatabaseShardingAlgorithm {
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, Collection<ShardingValue<?>> shardingValues) {
        if (shardingValues.isEmpty() || availableTargetNames.isEmpty()) {
            return null;
        }

        for (String each : availableTargetNames) {
            for (ShardingValue<?> shardingValue : shardingValues) {
                if (shardingValue.getColumnName().equals("type")) {
                    if (each.endsWith(Integer.parseInt(shardingValue.getValue().toString()) % 2 + "")) {
                        return Collections.singletonList(each);
                    }
                }
            }
        }
        throw new IllegalArgumentException();
    }
}
